//
//  MainFirst_MagazineList_TBV_WX.m
//  ZITHER
//
//  Created by weixin on 2018/10/11.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainFirst_MagazineList_TBV_WX.h"
#import "MainFirst_MagazineList_PGCCell_WX.h"
#import "MainFirst_MagazineList_UgcCell_WX.h"
#import "Ugc_Detail_VC_WX.h"
#import "MainFirst_Magazine_VC_WX.h"
#import "TopicDetailViewController.h"

@implementation MainFirst_MagazineList_TBV_WX

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self defaultConfiguration];
    }
    return self;
}

-(void)defaultConfiguration{
    
    self.delegate = self;
    self.dataSource = self;
    
    /** 给头部间隙 */
    self.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 20)];
    [self setLoad];

//    @weakify(self);
//    [RACObserve(self, page)subscribeNext:^(id  _Nullable x) {
//        @strongify(self);
//    }];

}

-(void)Starefreshing{
    [self setLoad];

}

-(void)setLoad{
    
    [self.mj_header endRefreshing];
    [self.mj_footer endRefreshing];
    
    @weakify(self);
    [[serviceDefualt loadGetCoverCreateListPage:self.page type:@"RECOMMEND" reponseObjctClass:[CoverCreateListModel class]]subscribeNext:^(ResponseData * x) {
        @strongify(self);
        
        if (x.success) {
            
            CoverCreateListModel * model = [[CoverCreateListModel alloc]init];
            model.recommend = @"0";
            CoverResultModelSub *mdoel1 = [CoverResultModelSub yy_modelWithDictionary:x.responseRawData[@"data"]];
            model.result = mdoel1;
            
//            CoverCreateListModel * model = x.responseData;
            
            if (model.result.list.count == 0) {
                
                if (self.page == 0 ) {
                    [Tools showTips:@"暂无更多数据"];
                    [self endRefreshing];

                }else{
                    [self.mj_footer endRefreshingWithNoMoreData];
                }
                return ;
                
            }else{
                if (self.page == 0) {
                    self.model = model;
                }else{
                    for (CoverCreateListModelSub *modelSub in model.result.list) {
                        [self.model.result.list addObject:modelSub];
                    }
                }
            }
            [self endRefreshing];
            [self reloadData];
        }else{
            [Tools showTips:x.errorInfo];
            [self endRefreshing];
        }
    }];

}

#pragma MARK - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.result.list.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* TODO: ================= 判断 cell 样式 =================*/
    
    CoverCreateListModelSub *modelSub = self.model.result.list[indexPath.row];
    
    if ([modelSub.relType isEqualToString:@"1"] ||[modelSub.relType isEqualToString:@"3"]) {
        static NSString *sectionCell = @"MainFirst_MagazineList_UgcCell_WX";
        MainFirst_MagazineList_UgcCell_WX *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
        if (cell == nil) {
            cell = [[MainFirst_MagazineList_UgcCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
        }
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        cell.model = modelSub;
        return cell;
    }else{
        static NSString *sectionCell = @"MainFirst_MagazineList_PGCCell_WX";
        MainFirst_MagazineList_PGCCell_WX *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
        if (cell == nil) {
            cell = [[MainFirst_MagazineList_PGCCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
        }
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        cell.model = modelSub;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CoverCreateListModelSub *modelSub = self.model.result.list[indexPath.row];
    if ([modelSub.relType isEqualToString:@"1"]) {
        Ugc_Detail_VC_WX *vc = [[Ugc_Detail_VC_WX alloc]init];
        
        vc.ugcId = modelSub.relId;
        vc.userID = modelSub.userId;
        [vc setBlockDel:^{
            [self.model.result.list removeObjectAtIndex:self.selectCellIndexPath];
            [self deleteRowsAtIndexPaths:@[indexPath]  withRowAnimation:UITableViewRowAnimationFade];        }];
        [self.getNav pushViewController:vc animated:YES];
    }else if([modelSub.relType isEqualToString:@"3"]){
        TopicDetailViewController * vc = [[TopicDetailViewController
                                           alloc]init];
        vc.topicId = modelSub.relId;
        [self.getNav pushViewController:vc animated:YES];
        
    }else{
        MainFirst_Magazine_VC_WX *vc = [[MainFirst_Magazine_VC_WX alloc]init];
        vc.pgcId = modelSub.relId;
        [self.getNav pushViewController:vc animated:YES];
    }
    
}

@end
